package gamePieces;




import gameComponents.Board;
import gameComponents.Player;

import java.awt.Point;
import java.util.ArrayList;

/**
 * Class for the chess piece of a knight.
 * @author Jordan
 *
 */
public class Knight extends Piece {
	/**
	 * Default constructor for Knight object.
	 * X and Y coordinates of Knight are set in the superclass Piece. 
	 * 
	 * @param x coordinate of Knight
	 * @param y coordinate of Knight
	 */
	public Knight(int x, int y, Player player) {
		super(x,y, player);
	}
	
	
	/**
      * Overrides abstract method in superclass Piece.
	  * Calculates list of all possible moves of
	  * the Knight object at its current position.
	  * Knight piece can move it can move two squares horizontally 
	  * and one square vertically, or two squares vertically and 
	  * one square horizontally.
	  * @return an ArrayList of all the possible points the knight can IDEALLY move to.
	  * @author Selman Selman
	 */
	public ArrayList<Point> possibleMoves(){
		//creates a new arraylist of points to add possible moves to it
		moves = new ArrayList<Point>(); 
		
		int x = super.getX(); //get the current x coordinate of knight
		int y = super.getY(); //get the current y coordinate of knight
		
		//makes sure each possible move is within the boundaries for the grid
		// adds the possible moves to the possMoves array
		if(x+1 < Board.SIZE && y+2 < Board.SIZE){ //check (x+1,y+2)
			moves.add(new Point(x+1, y+2));
		}
		if(x+1 < Board.SIZE && y-2>=0){ //check (x+1,y-2)
			moves.add(new Point(x+1,y-2));
		}
		if(x-1 >= 0 && y +2 < Board.SIZE){ //check (x-1,y+2)
			moves.add(new Point(x-1, y+2));
		}
		if(x-1 >= 0 && y-2>=0){ //check (x-1,y-2)
			moves.add(new Point(x-1, y-2));
		}
		if (y+1 < Board.SIZE && x+2 < Board.SIZE){ //check (x+2,y+1)
			moves.add(new Point(x+2,y+1));
		}
		if (y+1 < Board.SIZE && x-2 >= 0){ // check (x-2,y+1)
			moves.add(new Point(x-2,y+1));
		}
		if (y-1 >= 0 && x+2 < Board.SIZE){ //check (x+2,y-1)
			moves.add(new Point(x+2, y-1));
		}
		if (y-1>=0 && x-2 >= 0){ //check (x-2,y-1)
			moves.add(new Point(x-2,y-1));
		}
		return moves;
	}
	
	/**
	 * toString Method
	 * @return The string representation of a Knight on the board
	 * @author Vivian Andreeva
	 * @modified by Selman Selman
	 */
	public String toString() {
		if (getOwnership().getId()==1) {
		return "N";
		}
		return "n";
	}
	
	/**
	 * Checks if the attempted move is legal for the knight
	 * @return true if the move is legal.
	 * @author Selman Selman, Jordan Beland
	 */
	public boolean checkIfLegal(int x, int y){
		if(x < 0 || x > Board.SIZE){
			return false;
		} else if(y < 0 || y > Board.SIZE){
			return false;
		} else{
			int xDiff = Math.abs(super.getX() - x);
			int yDiff = Math.abs(super.getY() - y);
			if (xDiff == 1 && yDiff == 2){
				return true;
			} else if (xDiff == 2 && yDiff == 1){
				return true;
			} else{
				return false;
			}
		}
	}
}